package com.wangjiyuan.giftshuo.ui.fragment.classify.strategy;

import android.util.Log;

import com.wangjiyuan.giftshuo.bean.classifybean.strategy.ColumnBean;
import com.wangjiyuan.giftshuo.bean.classifybean.strategy.GroupBean;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by QXL on 2016/11/15.
 */

public class StrategyPresenter implements StrategyContract.IStrategyPresenter {
    private StrategyContract.IStrategyModel mStrategyModel;
    private StrategyContract.IStrategyView mStrategyFragment;
    private Subscription subscribe;

    public StrategyPresenter(StrategyContract.IStrategyView view){
        mStrategyModel=new StrategyModel();
        this.mStrategyFragment=view;
    }

    /**
     * 获取Column的Observable
     */
    @Override
    public void getColumnObservable() {
        Observable<ColumnBean> columObservable = mStrategyModel.getColumObservable();
        subscribe=columObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ColumnBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {
//                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
//                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onNext(ColumnBean columnBean) {
                        mStrategyFragment.loadColumnBean(columnBean);
                    }
                });
    }

    /**
     * 获取Group的Observable
     */
    @Override
    public void getGroupObservable() {
        Observable<GroupBean> groupObservable = mStrategyModel.getGroupObservable();
        subscribe = groupObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<GroupBean>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        mStrategyFragment.showProgress();
                    }

                    @Override
                    public void onCompleted() {
                        mStrategyFragment.hideProgress();
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mStrategyFragment.showProgress();
                        mStrategyFragment.hideProgress();
                        subscribe.unsubscribe();
                    }

                    @Override
                    public void onNext(GroupBean groupBean) {
                        mStrategyFragment.loadGroupBean(groupBean);
                    }
                });
    }
}
